package controller;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import service.WishManageService;
import dto.ListWishPageDTO;
import dto.MemberDTO;
import dto.ProductDTO;
import dto.WishDTO;

@Controller
public class WishController {
	private WishManageService service;
	public WishController(WishManageService service) {
		this.service = service;
	}
	@RequestMapping("insertWish.do")
	public ModelAndView insertWish(int productId, HttpSession session){
		ModelAndView mv = null;
		MemberDTO member = (MemberDTO) session.getAttribute("memberDTO");
		WishDTO wishDTO = null;
		try{
			if(service.selectWishByIds(productId, member.getId()) != null){
				mv = new ModelAndView("wishSuccessTiles.do","message","이미 관심상품 (찜)에 있는 상품(쿠폰)입니다.");
			}
			else{
				wishDTO = new WishDTO(0, member.getId(),productId); 
				service.insertWish(wishDTO);
				mv = new ModelAndView("wishSuccessTiles.do","message","관심상품 (찜)에 해당 상품(쿠폰)이 추가되었습니다.");
			}
		}
		catch(Exception e){
			e.printStackTrace();
			mv = new ModelAndView("error.do","error_message",e.getMessage());
		}
		return mv;
	}
	@RequestMapping("removeWish.do")
	public ModelAndView removeWish(int productId, HttpSession session){
		ModelAndView mv = null;
		MemberDTO member= (MemberDTO) session.getAttribute("memberDTO");
		try {
			service.deleteWish(productId, member.getId());
			mv = new ModelAndView("wishSuccessTiles.do", "message","관심 상품이 정상적으로 삭제 되었습니다.");
		} catch (Exception e) {
			e.printStackTrace();
			mv = new ModelAndView("error.do","error_message",e.getMessage());
		}
		return mv;
	}
	@RequestMapping("WishAllList.do")
	public ModelAndView selectWishAllList(HttpSession session){
		ModelAndView mv = null;
		MemberDTO member = (MemberDTO) session.getAttribute("memberDTO");
		try {
			if(member==null){
				mv = new ModelAndView("wishList.do", "message", "로그인을 먼저 해주세요.");
			}
			else{
				ArrayList<ProductDTO> productList = service.selectProductByMemberId(member.getId());
				mv = new ModelAndView("wishList.do", "productList", productList);
			}
		} catch (Exception e) {
			e.printStackTrace();
			mv = new ModelAndView("error.do","error_message",e.getMessage());
		}
		return mv;
	}
	
	@RequestMapping("selectWishListByPage.do")
	public ModelAndView selectWishListByList(HttpServletRequest request, HttpSession session){
		ModelAndView mv= null;
		ListWishPageDTO listWishDTO = null;
		MemberDTO member = (MemberDTO) session.getAttribute("memberDTO");
		int page=1;
		try {
			if(request.getParameter("page")!=null){
				page= Integer.parseInt(request.getParameter("page"));
			}
			else{
				page=1;
			}
			listWishDTO = service.selectWishListByPage(page, member.getId());
			mv = new ModelAndView("wishList.do","listWishDTO",listWishDTO);
			request.setAttribute("page", page);
		} catch (Exception e) {
			e.printStackTrace();
			mv = new ModelAndView("error.do","error_message",e.getMessage());
		}
		
		return mv;
	}
	
	
}
